﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Activities;
using System.Diagnostics;

namespace TfsExtensions.Build.Activities
{
    public sealed class InvokeDBCSFix : CodeActivity
    {
        [RequiredArgument]
        public InArgument<string> SandcastleProductionToolsFolder { get; set; }

        [RequiredArgument]
        public InArgument<string> SandcastleLocalLanguageCode { get; set; }

        [RequiredArgument]
        public InArgument<string> ChmFolderName { get; set; }

        // If your activity returns a value, derive from CodeActivity<TResult>
        // and return the value from the Execute method.
        protected override void Execute(CodeActivityContext context)
        {
            //  <Exec Command="&quot;$(DbcsFixEXE)&quot; /d:&quot;$(ChmFolderName)&quot; /l:$(SandcastleLocalLanguageCode)" />

            var exeFileName = string.Format(@"{0}\DBCSFix.exe", this.SandcastleProductionToolsFolder.Get(context));

            var chmFolderName = "/d:\"" + string.Format(@"{0}", this.ChmFolderName.Get(context)) + "\"";

            var sandcastleLanguageCode = "/l:" + string.Format("{0}", this.SandcastleLocalLanguageCode.Get(context));

            var arguments = string.Format("{0} {1}", chmFolderName, sandcastleLanguageCode);

            try
            {
                InvokeCommand.ExecuteCommand(context, exeFileName, arguments);
            }
            catch(Exception ex)
            {
                LogHelper.LogError(context, ex.Message);
                throw;
            }
        }
    }
}
